﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MeTalker.Window.SDK.Core;
using MeTalker.SDK.Core;
using MeTalker.Aide.Forms;
using System.Windows.Forms;
using System.Threading;

namespace MeTalker.Aide
{
    internal abstract class ServiceBase
    {
        protected IntPtr hwndMain;

        protected IntPtr buttonHandle;

        protected IntPtr mainhWndResposneContent;

        protected IntPtr mainhWndContent;

        protected System.Threading.Timer mTimerClose;

        ProcInfo proc;

        protected void Init(ProcInfo proc)
        {
            this.proc = proc;
            //查找主窗口
            hwndMain = proc.ChildhWnd;

            //查找按钮
            IntPtr buttonMainStandardWindow = WindowControl.FindWindowEx(hwndMain, WindowControl.FindWindowEx(hwndMain, WindowControl.FindWindowEx(hwndMain, IntPtr.Zero, "StandardWindow", null), "StandardWindow", null), "StandardWindow", null);
            buttonHandle = WindowControl.FindWindowEx(buttonMainStandardWindow, IntPtr.Zero, "StandardButton", null);

            IntPtr splithWnd = WindowControl.FindWindowEx(buttonMainStandardWindow, IntPtr.Zero, "SplitterBar", null);
            mainhWndContent = WindowControl.FindWindowEx(WindowControl.FindWindowEx(splithWnd, WindowControl.FindWindowEx(splithWnd, IntPtr.Zero, "StandardWindow", null), "StandardWindow", null), IntPtr.Zero, "RichEditComponent", null);

            mainhWndResposneContent = WindowControl.FindWindowEx(WindowControl.FindWindowEx(WindowControl.FindWindowEx(WindowControl.FindWindowEx(WindowControl.FindWindowEx(splithWnd, IntPtr.Zero, "StandardWindow", null), IntPtr.Zero, "SimpleBrowser", null), IntPtr.Zero, "Shell Embedding", null), IntPtr.Zero, "Shell DocObject View", null), IntPtr.Zero, "Internet Explorer_Server", null);
        }

        protected bool FindhWndCloseAlter(IntPtr hwnd, int a)
        {
            try
            {
                StringBuilder builder = new StringBuilder(0x100);
                WindowControl.GetClassName(hwnd, builder, 0x100);
                StringBuilder builder2 = new StringBuilder(0x100);
                WindowControl.GetWindowText(hwnd, builder2, 0x100);

                string className = builder.ToString();
                string title = builder2.ToString();
                if (className.Equals("StandardButton") && title.Equals("取消"))
                {
                    if (WindowControl.IsWindowVisible(hwnd))
                    {
                        if (UtilHelper.DEBUG)
                            MuchInfo.Logs.MILog.Info("关闭了对话框,取消并结束会话");
                        Console.WriteLine("关闭了对话框,取消");
                        MouseControl.MouseLeft(hwnd, 2, 2);
                        WindowControl.SetForegroundWindow(hwndMain);
                        KeysetControl.keybd_event(13, 0, 0, 0);
                        return false;
                    }
                }
            }
            catch { }

            return true;
        }

        void CloseCallback(object sender)
        {
            try
            {
                IntPtr hwndMain = WindowControl.FindWindow(null, "系统提示");
                int pid;
                WindowControl.GetWindowThreadProcessId(hwndMain, out pid);
                if (pid == proc.Pid)
                {
                    if (UtilHelper.DEBUG)
                        MuchInfo.Logs.MILog.Info("关闭对话框");
                    int d = WindowControl.EnumChildWindows(hwndMain, FindhWndCloseAlter, 0);
                }
            }
            catch { }
        }

        protected bool CloseSession()
        {
            try
            {
                if (mTimerClose != null)
                {
                    mTimerClose.Dispose();
                    mTimerClose = null;
                }
                mTimerClose = new System.Threading.Timer(CloseAlter, null, 0, 200);
                MouseControl.MouseLeft(buttonHandle, 2, 2);
                if (mTimerClose != null)
                {
                    mTimerClose.Dispose();
                    mTimerClose = null;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        void CloseAlter(object sender)
        {
            try
            {
                WindowControl.EnumChildWindows(WindowControl.FindWindow(null, "系统提示"), CloseAlter, 0);
            }
            catch { }
        }

        protected bool CloseAlter(IntPtr hwnd, int a)
        {
            try
            {
                StringBuilder builder = new StringBuilder(0x100);
                WindowControl.GetClassName(hwnd, builder, 0x100);
                StringBuilder builder2 = new StringBuilder(0x100);
                WindowControl.GetWindowText(hwnd, builder2, 0x100);

                string className = builder.ToString();
                string title = builder2.ToString();
                if (className.Equals("StandardButton") && title.Equals("确定"))
                {
                    if (UtilHelper.DEBUG)
                        MuchInfo.Logs.MILog.Info("关闭了对话框,取消并结束会话");
                    Console.WriteLine("关闭了对话框,取消");
                    MouseControl.MouseLeft(hwnd, 2, 2);
                    Thread.Sleep(10);
                }
            }
            catch { }

            return true;
        }
        
        protected void SendText(IntPtr intptr, string msg)
        {
            WangHepler.Send(hwndMain, intptr, msg, false);
            KeysetControl.SendMessage(intptr, KeysetControl.WM_KEYDOWN, KeysetControl.VK_RETURN, 0);
            KeysetControl.SendMessage(intptr, KeysetControl.WM_KEYUP, KeysetControl.VK_RETURN, 0); 
            for (int i = 0; i < 20; i++)
            {
                if (string.IsNullOrEmpty(WangHepler.EqualsMsg(intptr)))break;
                KeysetControl.SendMessage(intptr, KeysetControl.WM_KEYDOWN, KeysetControl.VK_RETURN, 0);
                KeysetControl.SendMessage(intptr, KeysetControl.WM_KEYUP, KeysetControl.VK_RETURN, 0); 
                Thread.Sleep(100);
            }
        }

        protected string GetText(IntPtr hWnd)
        {
            return LibHelper.GetMessageText(hWnd);
        }

        protected string GetText()
        {
            return GetText(mainhWndResposneContent);
        }
    }
}